package com.bian.spring.framework.context.support;

import com.bian.spring.framework.beans.factory.support.BeanDefinitionReader;
import com.bian.spring.framework.beans.factory.support.BeanDefinitionRegistry;
import com.bian.spring.framework.context.ApplicationContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ApplicationContext接口的子实现类，用于立即加载
 *
 * @author bian
 * 2021/01/22
 */
public abstract class AbstractApplicationContext implements ApplicationContext {
    
    // 定义用于存储bean对象的Map容器
    protected Map<String, Object> singletonObjects = new ConcurrentHashMap<>();
    
    // 声明解析器，用来解析bean的定义信息
    protected BeanDefinitionReader beanDefinitionReader;
    
    // 声明配置文件路径的变量
    protected String configLocation;
    
    @Override
    public void refresh() throws Exception {
        // 加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        // 初始化bean
        finishBeanInitialization();
    }

    // bean的初始化
    private void finishBeanInitialization() throws Exception {
        // 获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        // 获取BeanDefinition对象
        String[] beanNames = registry.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            // 进行bean的初始化
            getBean(beanName);
        }
    }
}
